//
//  WriteNoteController.m
//  ProductB
//
//  Created by 顾佳辉 on 16/5/31.
//  Copyright © 2016年 ChenZheng. All rights reserved.
//

#import "WriteNoteController.h"
#import "writeNoteTableViewCell.h"
#import "SearchViewController.h"
#define MainColor [UIColor colorWithRed:62.0/255.0 green:162.0/255.0 blue:232.0/255.0 alpha:1.0]
@interface WriteNoteController ()<UITableViewDelegate,UITableViewDataSource,UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong)UITableView *tableView;

@property (strong, nonatomic) UIPickerView *datePicker;
@property (strong, nonatomic) UIView *timeSelectView;
@property (strong, nonatomic) UIButton *cancelSelectTimeButton;
@property (strong, nonatomic) UIButton *determineSelectTimeButton;
@property (strong, nonatomic) NSString  *timeSelectedString;

@property (strong, nonatomic) writeNoteTableViewCell *cell;

@property (strong, nonatomic) UIButton *objectButton;

@end



@implementation WriteNoteController
{
    
    NSMutableArray *yearArray;
    NSArray *monthArray;
    NSMutableArray *monthMutableArray;
    NSMutableArray *DaysMutableArray;
    NSMutableArray *DaysArray;
    NSString *currentMonthString;
    
    NSInteger selectedYearRow;
    NSInteger selectedMonthRow;
    NSInteger selectedDayRow;
    
    BOOL firstTimeLoad;
    
    NSInteger m ;
    int year;
    int month;
    int day;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kWidth , kHeight - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:212/255.0 green:212/255.0 blue:212/255.0 alpha:1];
    [self.tableView registerNib:[UINib nibWithNibName:@"writeNoteTableViewCell" bundle:nil] forCellReuseIdentifier:@"write"];
    self.tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    m=0;
    firstTimeLoad = YES;
    NSDate *date = [NSDate date];
    
    // Get Current Year
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    
    NSString *currentyearString = [NSString stringWithFormat:@"%@",
                                   [formatter stringFromDate:date]];
    year =[currentyearString intValue];
    
    
    // Get Current  Month
    
    [formatter setDateFormat:@"MM"];
    
    currentMonthString = [NSString stringWithFormat:@"%ld",(long)[[formatter stringFromDate:date]integerValue]];
    month=[currentMonthString intValue];
    
    
    
    
    // Get Current  Date
    
    [formatter setDateFormat:@"dd"];
    NSString *currentDateString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
    
    day =[currentDateString intValue];
    
    
    yearArray = [[NSMutableArray alloc]init];
    monthMutableArray = [[NSMutableArray alloc]init];
    DaysMutableArray= [[NSMutableArray alloc]init];
    for (int i = 1970; i <= year ; i++)
    {
        [yearArray addObject:[NSString stringWithFormat:@"%d",i]];
        
    }
    
    
    // PickerView -  Months data
    
    
    monthArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
    
    for (int i=1; i<month+1; i++) {
        [monthMutableArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    DaysArray = [[NSMutableArray alloc]init];
    
    for (int i = 1; i <= 31; i++)
    {
        [DaysArray addObject:[NSString stringWithFormat:@"%d",i]];
        
    }
    for (int i = 1; i <day+1; i++)
    {
        [DaysMutableArray addObject:[NSString stringWithFormat:@"%d",i]];
        
    }
    
    // PickerView - Default Selection as per current Date
    // 设置初始默认值
    [self.datePicker selectRow:20 inComponent:0 animated:YES];
    
    // [pickerView selectRow:30 inComponent:0 animated:NO];
    
    [self.datePicker selectRow:[monthArray indexOfObject:currentMonthString] inComponent:1 animated:YES];
    
    [self.datePicker selectRow:0 inComponent:2 animated:YES];

    [self.view addSubview:self.timeSelectView];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    writeNoteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"write" forIndexPath:indexPath];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    [cell.date addTarget:self action:@selector(timeSelectedButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [cell.mudidi addTarget:self action:@selector(SearchFor:) forControlEvents:UIControlEventTouchUpInside];
    self.objectButton = cell.mudidi;
    self.cell = cell;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 604;
}

//查找目的地
- (void)SearchFor:(UIButton *)sender
{
    SearchViewController *searchVc = [[SearchViewController alloc]init];
    searchVc.blockName = ^(NSString *name)
    {
        [self.objectButton setTitle:name forState:UIControlStateNormal];
        [self.objectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:searchVc animated:YES];
}




-(void)timeSelectedButtonAction{
  
    
    [self.view addSubview:self.timeSelectView];
    [UIView animateWithDuration:0.3 animations:^{
        self.timeSelectView.frame = CGRectMake(0, kHeight-315, kWidth, 316);
    }];
}



#pragma  mark - timeSelected
-(UIView *)timeSelectView{
    if (!_timeSelectView) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kWidth, 50)];
        label.text = @"时间选择";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        
        _timeSelectView = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight, kWidth, 316)];
        _timeSelectView.backgroundColor = [UIColor whiteColor];
        [_timeSelectView addSubview:label];
        [_timeSelectView addSubview:self.datePicker];
        [_timeSelectView addSubview:self.cancelSelectTimeButton];
        [_timeSelectView addSubview:self.determineSelectTimeButton];
    }
    return _timeSelectView;
}
-(UIPickerView *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, kWidth, 150)];
        _datePicker.backgroundColor = [UIColor whiteColor];
        _datePicker.dataSource = self;
        _datePicker.delegate = self;
    }
    return _datePicker;
}

-(UIButton *)cancelSelectTimeButton{
    if (!_cancelSelectTimeButton) {
        _cancelSelectTimeButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 200, 80, 40)];
        _cancelSelectTimeButton.backgroundColor = MainColor;
        _cancelSelectTimeButton.layer.cornerRadius = 5;
        _cancelSelectTimeButton.layer.masksToBounds = YES;
        [_cancelSelectTimeButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelSelectTimeButton addTarget:self action:@selector(cancelSelectTimeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelSelectTimeButton;
}


-(UIButton *)determineSelectTimeButton{
    if (!_determineSelectTimeButton) {
        _determineSelectTimeButton = [[UIButton alloc]initWithFrame:CGRectMake(kWidth-110, 200, 80, 40)];
        _determineSelectTimeButton.backgroundColor = MainColor;
        _determineSelectTimeButton.layer.cornerRadius = 5;
        _determineSelectTimeButton.layer.masksToBounds = YES;
        [_determineSelectTimeButton setTitle:@"确定" forState:UIControlStateNormal];
        [_determineSelectTimeButton addTarget:self action:@selector(determineSelectTimeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _determineSelectTimeButton;
}

-(void)cancelSelectTimeButtonAction{
    [UIView animateWithDuration:0.3 animations:^{
        self.timeSelectView.frame = CGRectMake(0, kHeight, kWidth, 316);
    }];
    
}


-(void)determineSelectTimeButtonAction{
    [self.cell.date setTitle:[NSString stringWithFormat:@"%@年%@月%@日 ",[yearArray objectAtIndex:[self.datePicker selectedRowInComponent:0]],[monthArray objectAtIndex:[self.datePicker selectedRowInComponent:1]],[DaysArray objectAtIndex:[self.datePicker selectedRowInComponent:2]]] forState:UIControlStateNormal] ;
    [self.cell.date setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [UIView animateWithDuration:0.3 animations:^{
        self.timeSelectView.frame = CGRectMake(0, kHeight, kWidth, 316);
    }];
    
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    m=row;
    
    
    
    if (component == 0)
    {
        selectedYearRow = row;
        [self.datePicker reloadAllComponents];
    }
    else if (component == 1)
    {
        selectedMonthRow = row;
        [self.datePicker reloadAllComponents];
    }
    else if (component == 2)
    {
        selectedDayRow = row;
        
        [self.datePicker reloadAllComponents];
        
    }
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component
           reusingView:(UIView *)view {
    
    // Custom View created for each component
    
    UILabel *pickerLabel = (UILabel *)view;
    
    if (pickerLabel == nil) {
        CGRect frame = CGRectMake(0.0, 0.0, 50, 60);
        pickerLabel = [[UILabel alloc] initWithFrame:frame];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:15.0f]];
    }
    
    
    
    if (component == 0)
    {
        pickerLabel.text =  [yearArray objectAtIndex:row]; // Year
    }
    else if (component == 1)
    {
        pickerLabel.text =  [monthArray objectAtIndex:row];  // Month
    }
    else if (component == 2)
    {
        pickerLabel.text =  [DaysArray objectAtIndex:row]; // Date
        
    }
    
    
    return pickerLabel;
    
}



- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0)
    {
        return [yearArray count];
        
    }
    else if (component == 1)
    {
        NSInteger selectRow =  [pickerView selectedRowInComponent:0];
        int n;
        n= year-1970;
        if (selectRow==n) {
            return [monthMutableArray count];
        }else
        {
            return [monthArray count];
            
        }
    }
    else
    {
        NSInteger selectRow1 =  [pickerView selectedRowInComponent:0];
        int n;
        n= year-1970;
        NSInteger selectRow =  [pickerView selectedRowInComponent:1];
        
        if (selectRow==month-1 &selectRow1==n) {
            
            return day;
            
        }else{
            
            if (selectedMonthRow == 0 || selectedMonthRow == 2 || selectedMonthRow == 4 || selectedMonthRow == 6 || selectedMonthRow == 7 || selectedMonthRow == 9 || selectedMonthRow == 11)
            {
                return 31;
            }
            else if (selectedMonthRow == 1)
            {
                int yearint = [[yearArray objectAtIndex:selectedYearRow]intValue ];
                
                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){
                    return 29;
                }
                else
                {
                    return 28; // or return 29
                }
                
                
                
            }
            else
            {
                return 30;
            }
            
            
        }
        
    }
    

}

//- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    if (component == 0) {
//        return  self.myArray[row];
//    }else {
//        return  self.myArray1[row];
//    }
//}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
